<?php
/* @var $this PccongtackhacController */
/* @var $model Pccongtackhac */

$this->breadcrumbs=array(
	'Phân công công tác khác'=>array('admin'),
	'Phân công mới',
);

$this->menu=array(
	array('label'=>'Phân công giảng dạy', 'url'=>array('phancong/admin')),
	array('label'=>'Xem phân công', 'url'=>array('phancong/xemphancong')),
	array('label'=>'Phân công công tác khác', 'url'=>array('admin')),
	array('label'=>'Quản lý công tác khác', 'url'=>array('congtackhac/admin')),
	//array('label'=>'Quản lý thời khóa biểu', 'url'=>array('qhmonhom/admin')),
);
?>

<?php //$this->renderPartial('_form', array('model'=>$model)); ?>

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'pccongtackhac-form',
	// Please note: When you enable ajax validation, make sure the corresponding
	// controller action is handling ajax validation correctly.
	// There is a call to performAjaxValidation() commented in generated controller code.
	// See class documentation of CActiveForm for details on this.
	'enableAjaxValidation'=>false,
	'method'=>'post',
)); ?>
	<table>
	<th>
		<td><?php echo $form->labelEx($model,'namhoc'); ?></td>
		<td style="width: 40%; padding-left:17px;"><?php //echo CHtml::dropDownList('namhoc','',array((string)Yii::app()->request->cookies['year']=>(string)Yii::app()->request->cookies['year'].' - '.((string)Yii::app()->request->cookies['year']+1)),array('disabled'=>'disabled'));
			echo CHtml::dropDownList('namhoc','',array((string)Yii::app()->request->cookies['year']=>(string)Yii::app()->request->cookies['year'].' - '.((string)Yii::app()->request->cookies['year']+1)),array('name'=>'namhoc','id'=>'namhoc','disabled'=>'disabled'));		?></td>
		<td><?php echo $form->labelEx($model,'magv'); ?></td>
		<td style="padding-left:17px;"><?php 
			$connection=Yii::app()->db;   // assuming you have configured a "db" connection
			$command=$connection->createCommand()
			->select('magv,tengv')
			->from('giangvien')
			//->where(array('and','username="'.$this->username.'"','level>0'))
			->query();
			$giangvien= array();
			foreach($command as $row)
			{
				//array_push($ngach,$row);
				$giangvien[$row['magv']]=$row['tengv'];
			}
			echo CHtml::dropDownList('magv','',$giangvien); 
		?></td>	
	</th>
	</table>
	<br>
	<h1><center>Phân công công tác khác</center></h1>
	<br>
	<?php echo $form->errorSummary($model); ?>
	<div class='Table'>
	<div class='Heading'>
		<div class='Cell' style='width:4em'><center><?php //echo $form->label($model,'STT',array('hidden'=>'hidden')); ?></center></div>
		<div class='Cell' style='width:10em'><center><?php echo $form->label($model,'maCTK'); ?></center></div>
		<div class='Cell' style='width:12.5em'><center><?php echo $form->label($model,'ghichu'); ?></center></div>
		<div class='Cell' style='width:4em'></div>
	</div>
	</div>
	<div id='input1' class='clonedInput'>
	<?php echo CHtml::textField('0_STT','',array('id'=>'0_STT','hidden'=>'hidden')); ?>
	&emsp;&emsp;&emsp;&emsp;&emsp;&ensp;<?php 
	$command=$connection->createCommand()
			->select('maCTK,tenCTK')
			->from('congtackhac')
			//->where(array('and','username="'.$this->username.'"','level>0'))
			->query();
			$ctk= array();
			$ctk['rong']='';
			foreach($command as $row)
			{
				//array_push($ngach,$row);
				$ctk[$row['maCTK']]=$row['tenCTK'];
			}
	echo CHtml::dropDownList('0_maCTK','',$ctk,array('id'=>'0_maCTK')); ?> 
	&emsp;&emsp;&emsp;<?php echo CHtml::textField('0_ghichu','',array('id'=>'0_ghichu')); ?>
	&emsp;&emsp;&emsp;<?php echo CHtml::button('Xóa',array('id'=>'btnDel','class'=>'btnDel')); ?>
	</div>
	<div class='Table' style='width: 86.4%'>
	<div class='Row'>
		<div class='Cell' style='witdh:13em'><?php echo CHtml::textField('soluong','1',array('id'=>'soluong','hidden'=>'hidden')); ?></div>
		<div class='Cell' style='witdh:13em'><?php 
		$connection=Yii::app()->db;
	$command=$connection->createCommand()
			->select('maCTK,tenCTK')
			->from('congtackhac')
			//->where(array('and','username="'.$this->username.'"','level>0'))
			->query();
			//$ctk['rong']='';
			$sel='&emsp;&emsp;&emsp;&emsp;&emsp;&ensp;';
			$sel.='<select id="0_maCTK" name="0_maCTK">';
			$sel.='<option value ="rong" ></option>';
			foreach($command as $row)
			{
				//array_push($ngach,$row);
				//$ctk[$row['maCTK']]=$row['tenCTK'];
				$sel.='<option value ="'.$row['maCTK'].'" >'.$row['tenCTK'].'</option>';
			}
			$sel.='</select>';
			//var sel='<select id="[0]maCTK" name="[0]maCTK>"';
			//echo $sel;
		echo CHtml::textField('select',$sel,array('id'=>'select','hidden'=>'hidden')); ?></div>
		<div class='Cell' style='witdh:13em'></div>
		<div class='Cell' style='witdh:13em'></div>
		<div class='Cell' style='width:4em' align='right'><?php echo CHtml::button('Thêm',array('id'=>'btnAdd')); ?></div>
	</div>
	</div>
	
	<div class="row buttons">
		<?php echo CHtml::submitButton($model->isNewRecord ? 'Tạo mới' : 'Lưu', array('id'=>'btnSave')); ?>
	</div>

	<style type="text/css">
    .Table
    {
        display: table;
    }
    .Title
    {
        display: table-caption;
        text-align: center;
        font-weight: bold;
        font-size: larger;
    }
    .Heading
    {
        display: table-row;
        font-weight: bold;
        text-align: center;
    }
    .Row
    {
        display: table-row;
    }
    .Cell
    {
        display: table-cell;
        border: none;
        border-width: thin;
        padding-left: 5px;
        padding-right: 5px;
    }
	</style>
	
<script type="text/javascript">
$(document).ready(function() {

    var inputs = 1;
	var $dem=1;
	var $php1='<?php echo CHtml::textField('0_STT','',array('id'=>'0_STT','hidden'=>'hidden')); ?>';
	var $php2=$('#select').val();
	var $php3='&emsp;&emsp;&emsp;&ensp;<?php echo CHtml::textField('0_ghichu','',array('id'=>'0_ghichu')); ?>';
	var $php4='&emsp;&emsp;&emsp;<?php echo CHtml::button('Xóa',array('id'=>'btnDel','class'=>'btnDel','name'=>'yt0')); ?>';
	var $str="<div id='input1' class='clonedInput'>"
			+$php1
			+$php2
			+$php3
			+$php4
			+"</div>";
	
	
    $('#btnAdd').click(function() {
        if(($('.clonedInput').length  ==0))
		{
			$('.Table:first').after($str);
			$dem=1;
			$('#soluong').val($dem);
		}
		else{
			$('.btnDel:disabled').removeAttr('disabled');
			var c = $('.clonedInput:first').clone(true);
			
			c.children(':first').attr('id',$('.clonedInput:last').children(':first').attr('id').replace($('.clonedInput:last').children(':first').attr('id').charAt(0),Number($('.clonedInput:last').children(':first').attr('id').charAt(0))+1) );
			c.children(':nth-child(2)').attr('id',$('.clonedInput:last').children(':nth-child(2)').attr('id').replace($('.clonedInput:last').children(':nth-child(2)').attr('id').charAt(0),Number($('.clonedInput:last').children(':nth-child(2)').attr('id').charAt(0))+1) );
			c.children(':nth-child(3)').attr('id',$('.clonedInput:last').children(':nth-child(3)').attr('id').replace($('.clonedInput:last').children(':nth-child(3)').attr('id').charAt(0),Number($('.clonedInput:last').children(':nth-child(3)').attr('id').charAt(0))+1) );
			
			c.children(':first').attr('name',$('.clonedInput:last').children(':first').attr('name').replace($('.clonedInput:last').children(':first').attr('name').charAt(0),Number($('.clonedInput:last').children(':first').attr('name').charAt(0))+1) );
			c.children(':nth-child(2)').attr('name',$('.clonedInput:last').children(':nth-child(2)').attr('name').replace($('.clonedInput:last').children(':nth-child(2)').attr('name').charAt(0),Number($('.clonedInput:last').children(':nth-child(2)').attr('name').charAt(0))+1) );
			c.children(':nth-child(3)').attr('name',$('.clonedInput:last').children(':nth-child(3)').attr('name').replace($('.clonedInput:last').children(':nth-child(3)').attr('name').charAt(0),Number($('.clonedInput:last').children(':nth-child(3)').attr('name').charAt(0))+1) );
			
			$('.clonedInput:last').after(c);
			++$dem;
			$('#soluong').val($dem);
			//get id của hidden field, .value="số lượng"
		}
    });

	$('body').on('click', '.btnDel', function(){
		if (confirm('Xác nhận xóa?')) {
			--$dem;
            --inputs;
            $(this).closest('.clonedInput').remove();
			$('#soluong').val($dem);
			var i=0;
			while(i<$dem)
			{
				if(i==0)
				{
					$('.clonedInput:first').children(':nth-child(2)').attr('id',$('.clonedInput:last').children(':nth-child(2)').attr('id').replace($('.clonedInput:last').children(':nth-child(2)').attr('id').charAt(0),0) );
					$('.clonedInput:first').children(':nth-child(1)').attr('id',$('.clonedInput:last').children(':nth-child(1)').attr('id').replace($('.clonedInput:last').children(':nth-child(1)').attr('id').charAt(0),0) );
					$('.clonedInput:first').children(':nth-child(3)').attr('id',$('.clonedInput:last').children(':nth-child(3)').attr('id').replace($('.clonedInput:last').children(':nth-child(3)').attr('id').charAt(0),0) );
					
					$('.clonedInput:first').children(':nth-child(2)').attr('name',$('.clonedInput:last').children(':nth-child(2)').attr('name').replace($('.clonedInput:last').children(':nth-child(2)').attr('name').charAt(0),0) );
					$('.clonedInput:first').children(':nth-child(1)').attr('name',$('.clonedInput:last').children(':nth-child(1)').attr('name').replace($('.clonedInput:last').children(':nth-child(1)').attr('name').charAt(0),0) );
					$('.clonedInput:first').children(':nth-child(3)').attr('name',$('.clonedInput:last').children(':nth-child(3)').attr('name').replace($('.clonedInput:last').children(':nth-child(3)').attr('name').charAt(0),0) );
				}
				else{
					$('.clonedInput').eq(i).children(':nth-child(2)').attr('id',$('.clonedInput').eq(i-1).children(':nth-child(2)').attr('id').replace($('.clonedInput').eq(i-1).children(':nth-child(2)').attr('id').charAt(0),Number($('.clonedInput').eq(i-1).children(':nth-child(2)').attr('id').charAt(0))+1) );
					$('.clonedInput').eq(i).children(':nth-child(1)').attr('id',$('.clonedInput').eq(i-1).children(':nth-child(1)').attr('id').replace($('.clonedInput').eq(i-1).children(':nth-child(1)').attr('id').charAt(0),Number($('.clonedInput').eq(i-1).children(':nth-child(1)').attr('id').charAt(0))+1) );
					$('.clonedInput').eq(i).children(':nth-child(3)').attr('id',$('.clonedInput').eq(i-1).children(':nth-child(3)').attr('id').replace($('.clonedInput').eq(i-1).children(':nth-child(3)').attr('id').charAt(0),Number($('.clonedInput').eq(i-1).children(':nth-child(3)').attr('id').charAt(0))+1) );
					
					$('.clonedInput').eq(i).children(':nth-child(2)').attr('name',$('.clonedInput').eq(i-1).children(':nth-child(2)').attr('name').replace($('.clonedInput').eq(i-1).children(':nth-child(2)').attr('name').charAt(0),Number($('.clonedInput').eq(i-1).children(':nth-child(2)').attr('name').charAt(0))+1) );
					$('.clonedInput').eq(i).children(':nth-child(1)').attr('name',$('.clonedInput').eq(i-1).children(':nth-child(1)').attr('name').replace($('.clonedInput').eq(i-1).children(':nth-child(1)').attr('name').charAt(0),Number($('.clonedInput').eq(i-1).children(':nth-child(1)').attr('name').charAt(0))+1) );
					$('.clonedInput').eq(i).children(':nth-child(3)').attr('name',$('.clonedInput').eq(i-1).children(':nth-child(3)').attr('name').replace($('.clonedInput').eq(i-1).children(':nth-child(3)').attr('name').charAt(0),Number($('.clonedInput').eq(i-1).children(':nth-child(3)').attr('name').charAt(0))+1) );
				}
				i+=1;
			}
        }
	});

});
	
    </script>
	<?php $this->endWidget(); ?>

</div><!-- form -->